package com.lin.boot05webadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.boot05webadmin.bean.User;
import com.lin.boot05webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author ChenweiLin
 * @create 2021-04-02 22:06
 */
@Controller
public class TableController {

    @Autowired
    UserService userService;


    @GetMapping("/basic_table.html")
     public String basic_table(){

         return "table/basic_table";
     }

     @GetMapping("/user/delete/{id}")
     public String deleteUser(@PathVariable("id") long id , @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                              RedirectAttributes ra){ //RedirectAttributes ra 重定向参数

        userService.removeById(id);
        ra.addAttribute("pn", pn);
        return "redirect:/dynamic_table";
     }

    @GetMapping("/dynamic_table.html")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){

         //从数据库中查出user表中的用户进行展示
        List<User> list = userService.list();
//        model.addAttribute("users", list);
        //有输出  正面mp整合完毕
        for (User user : list) {
            System.out.println(user.toString());
        }

        //增加分页功能
        //分页数据查询
        Page<User> userPage = new Page<>(pn,2);//第一个参数：当前页码 第二个参数 每页条数
        //分页查询的结果
        Page<User> page = userService.page(userPage, null);//查询条件  queryWrapper参数：查询条件

        model.addAttribute("page", page);

        return "table/dynamic_table";
    }

    @GetMapping("/responsive_table.html")
    public String responsive_table(){

        return "table/responsive_table";
    }

    @GetMapping("/editable_table.html")
    public String editable_table(){

        return "table/editable_table";
    }
}
